package com.zj.osm.view.account.login

import android.text.TextUtils
import com.blankj.utilcode.util.SPUtils
import com.zj.utils.utils.http.HttpConfig
import org.xmlpull.v1.XmlPullParser
import org.xmlpull.v1.XmlPullParserFactory
import java.io.StringReader
import java.util.*

/**
 * @project OsmDroid
 * @author jiang zhu
 * @data 2019/12/6
 */
class LoginActivityModel {

    /**
     * 登录
     * @param account 账号
     * @param password 密码
     */
    fun toLogin(account: String, password: String): Boolean {
        val map = LinkedHashMap<String, Any>()
        //1.登陆接口 POST
        map["Username"] = account
        map["Password"] = password
        val result = HttpConfig.callService(0,"getwebuser", map)
        return if (!TextUtils.isEmpty(result) && parseXMLWithPull(result)) {
            SPUtils.getInstance().put("mUsername", account)
            SPUtils.getInstance().put("mPassword", password)
            true
        } else {
            false
        }
    }

    /**
     * 解析登录返回的xml
     * @param xmlData data
     */
    private fun parseXMLWithPull(xmlData: String): Boolean {
        var res = 1
        try {
            val factory = XmlPullParserFactory.newInstance()
            val xmlPullParser = factory.newPullParser()
            //设置输入的内容
            xmlPullParser.setInput(StringReader(xmlData))
            //获取当前解析事件，返回的是数字
            var eventType = xmlPullParser.eventType
            //保存内容
            var msg: String
            var usersId: String
            var name: String

            while (eventType != XmlPullParser.END_DOCUMENT) {
                val nodeName = xmlPullParser.name
                when (eventType) {
                    //开始解析XML
                    XmlPullParser.START_TAG -> {
                        //nextText()用于获取结点内的具体内容
                        if ("usersId" == nodeName) {
                            usersId = xmlPullParser.nextText()
                            //SharedPreferencesUtils.putString(getApplicationContext(), "userId", usersId);
                            SPUtils.getInstance().put("usersId", usersId)
                        }
                        if ("msg" == nodeName) {
                            msg = xmlPullParser.nextText()

                            if (msg == "成功") {
                                SPUtils.getInstance().put("islogin", "1")
                                res = 0
                            }
                        }
                        if ("token" == nodeName) {
                            val token = xmlPullParser.nextText()
                            SPUtils.getInstance().put("token", token)
                        }
                        if ("name" == nodeName) {
                            name = xmlPullParser.nextText()
                            SPUtils.getInstance().put("name", name)
                        }

                        /*else if("version".equals(nodeName))
                            version = xmlPullParser.nextText();*/
                    }
                    //结束解析
                    XmlPullParser.END_TAG -> {
                    }
                    else -> {
                    }
                }
                //下一个
                eventType = xmlPullParser.next()
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }

        return res == 0
    }


}